/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package swingDemoEvent;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class StudentSort {
 public static void main(String[] args) {
  Student stu1 = new Student("stu1",10,150);
  Student stu2 = new Student("stu2",9,140);
  Student stu3 = new Student("stu3",10,160);
  Student stu4 = new Student("stu4",10,140);
  Student stu5 = new Student("stu5",11,152);
  Student stu6 = new Student("stu6",12,151);
  Student stu7 = new Student("stu7",8,140);
  Student stu8 = new Student("stu8",10,156);
  Student stu9 = new Student("stu9",11,149);
  Student stu10 = new Student("stu10",10,155);
  Student stu11 = new Student("stu11",13,152);
  List<Student> list = new ArrayList<Student>();
  list.add(stu1);
  list.add(stu2);
  list.add(stu3);
  list.add(stu4);
  list.add(stu5);
  list.add(stu6);
  list.add(stu7);
  list.add(stu8);
  list.add(stu9);
  list.add(stu10);
  list.add(stu11);

  //比较大小
  Collections.sort(list);
  for(Student stu : list) {
   System.out.println(stu);
  }
 }

}
class Student implements Comparable<Student> {
 private int age;
 private int height;//身高
 private String name;
 public Student(String name, int age, int height) {
  this.name = name;
  this.age = age;
  this.height = height;
 }

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }

 public int getHeight() {
  return height;
 }

 public void setHeight(int height) {
  this.height = height;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }
 @Override
 public String toString() {
  return "Name:" + this.getName() + ";age:" + this.getAge() + ";height:" + this.getHeight();
 }


 public int compareTo(Student o) {
  //先按年龄比较，再按身高比较
 
   if(this.getAge() == o.getAge()) {
     return this.getHeight() - o.getHeight();
   }else{
      return this.getAge()-o.getAge(); 
   }
 }
}
